<?php
/**
 * Created by PhpStorm.
 * User: fengbob
 * Date: 2018/1/22
 * Time: 10:29
 */
namespace app\controllers;

use app\librarys\ApiResponseCode;
use app\librarys\BaseApiController;
use app\librarys\Tools;
use app\librarys\ZfbService;
use app\models\CategoryExt;
use app\models\db\Ad;
use app\models\db\Address;
use app\models\Area;
use app\models\db\Cart;
use app\models\db\Category;
use app\models\CategoryQuery;
use app\models\db\Favourite;
use app\models\db\Goods;
use app\models\db\Log;
use app\models\db\MiniPrograms;
use app\models\db\ZfbTradeRecord;
use app\models\GoodsBak;
use app\models\db\GoodsDetailHtml;
use app\models\db\GoodsImage;
use app\models\db\Order;
use app\models\db\OrderGoods;
use app\models\db\TUser;
use app\models\GoodsQuery;
use app\models\MiniProgramsExt;
use app\models\OrderExt;
use app\models\TUserExt;
use app\models\User;
use Faker\Provider\DateTime;
use Yii;
use yii\db\Exception;
use yii\db\Query;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\librarys\ExpressQuery;

class ApiController extends BaseApiController
{

    public function actionTest(){
        $request = \Yii::$app->request;

        $ic_num = $request->get('ic_num');
        $x = rand(1,1000);
        $data['result'] = 0;
        $data['img_url']='';
        $data['name']='钱钟书'.$x;
        $data['ic_num'] = $ic_num;
        $data['message'] = '刷卡成功！';
        return $this->renderJson($data,0,'ok');
    }


    public function actionTouristLogin(){
        $app_id = $this->_getRequestValue('app_id');
        $result = TUserExt::touristLogin($app_id);
        return $this->renderJson($result, 0, '成功!');
    }

    /**
     * login
     * @return string
     */
    public function actionLogin(){
        $auth_code = $this->_getRequestValue('auth_code');
        $app_id = $this->_getRequestValue('app_id');
        $nickName = $this->_getRequestValue('nickName','');
        $avatar = $this->_getRequestValue('avatar','');

        $result = TUserExt::login($app_id,$auth_code,$nickName,$avatar);
        return $this->renderJson($result, 0, '成功!');
    }

    public function actionZfbCallBack(){

        $request = Yii::$app->request;

//        var_dump($request->bodyParams);

        $record = new ZfbTradeRecord();

        $record->id = Tools::db_uuid();

        $record->notify_time = $request->post('notify_time','');
//        echo $record->notify_time;
        $record->notify_type = $request->post('notify_type','');
        $record->notify_id = $request->post('notify_id','');
        $record->app_id = $request->post('app_id','');
//        $record->app_id = $this->_getRequestValue('app_id');
//        var_dump($request->post('app_id',''));
        $record->charset = $request->post('charset','');
        $record->version = $request->post('version','');
        $record->sign_type = $request->post('sign_type','');
        $record->sign = $request->post('sign','');
//        $record->sign = $request->get()
        $record->trade_no = $request->post('trade_no','');
        $record->out_trade_no = $request->post('out_trade_no','');
        $record->out_biz_no = $request->post('out_biz_no', '');
        $record->buyer_id = $request->post('buyer_id', '');
        $record->buyer_logon_id = $request->post('buyer_logon_id', '');
        $record->seller_id = $request->post('seller_id', '');
        $record->seller_email = $request->post('seller_email', '');
        $record->trade_status = $request->post('trade_status', '');
        $record->total_amount = $request->post('total_amount', '');
        $record->receipt_amount = $request->post('receipt_amount', '');
        $record->invoice_amount = $request->post('invoice_amount', '');
        $record->buyer_pay_amount = $request->post('buyer_pay_amount', '');
        $record->point_amount = $request->post('point_amount', '');
        $record->refund_fee = $request->post('refund_fee', '');
        $record->subject = $request->post('subject', '');
        $record->body = $request->post('body', '');
        $record->gmt_create = $request->post('gmt_create', '');
        $record->gmt_payment = $request->post('gmt_payment', '');
        $record->gmt_refund = $request->post('gmt_refund', '');
        $record->gmt_close = $request->post('gmt_close', '');
        $record->fund_bill_list = $request->post('fund_bill_list', '');
        $record->passback_params = $request->post('passback_params', '');
        $record->voucher_detail_list = $request->post('voucher_detail_list', '');


        //

//        $record->sign = ZfbService::checkRsa($_POST,$record->app_id);
//        if($flag)


//            $result = $record->save();
        $result = OrderExt::orderSuccessPrecess($record);


//        else
//            $result = false;


//        voucher_detail_listvar_dump($record->app_id);
//        var_dump($result);


// 返回所有参数
//        $params = $request->bodyParams;
//        $log = new Log();
//        $log->id = Tools::db_uuid();
//        $log->class = 'ApiController.actionZfbCallBack';
//        $log->log = implode(",", $request->bodyParams);
//
////        $log->log = implode(",", $params);
//        $log->save();

        return $this->renderJson($result,0,$result);
    }



}